package com.itheima.dao;

import com.itheima.bean.User;
import com.itheima.exception.ExceptionEnum;
import com.itheima.exception.UserException;
import com.itheima.model.UserModel;

import java.util.List;

public class UserDaoImpl implements UserDao{

    private static String usersDataPath = "src/com/itheima/data/users.oxt";

    @Override
    public User login(String username, String password) throws Exception {
        return null;
    }

    @Override
    public void register(User user) throws Exception {
        // 1. 判断用户名是否存在
        final User userNow = UserModel.getUser(user.getUsername());

        // 2. 不存在，可以注册，保存用户
        if (userNow == null) {
            final List<User> users = UserModel.getUsers();
            users.add(user);
            UserModel.saveUsers(users);
            System.out.println("用户 " + user.getUsername() + " 注册成功.");
        } else {
            throw new UserException(ExceptionEnum.USER_REGISTER_ERROR, "用户已注册！");
        }
    }

    @Override
    public User findUserByUserName(String username) throws Exception {
        return UserModel.getUser(username);
    }

}
